<template>
  <div class="register-container">
    <div class="register-box">
      <h2 class="register-title">用户注册</h2>

      <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" @submit.prevent="handleRegister">
        <el-form-item prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="User" size="large" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码（至少8位且包含字母）" prefix-icon="Lock"
            size="large" show-password />
        </el-form-item>


        <el-form-item prop="Phone">
          <el-input v-model="registerForm.Phone" placeholder="请输入手机号" prefix-icon="Phone" size="large" />
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" prefix-icon="Lock" size="large" show-password />
        </el-form-item>

        <el-form-item prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱" prefix-icon="Message" size="large" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="large" class="register-button" :loading="loading" native-type="submit">
            注册
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-link">
        已有账号？<el-link type="primary" :underline="false" @click="goToLogin">立即登录</el-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  Phone: '',
  email: ''
})
const registerFormRef = ref(null)

// const countdown = ref(0)
// const isCountingDown = ref(false)
const loading = ref(false)

// 密码验证规则
const validatePassword = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入密码'))
  } else if (value.length < 8) {
    callback(new Error('密码长度不能小于8位'))
  } else if (!/[a-zA-Z]/.test(value)) {
    callback(new Error('密码必须包含至少一个字母'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, message: '用户名长度不能小于4个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePassword, trigger: 'blur' }
  ],
  Phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      }, trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}



// 注册提交
import axios from 'axios'
import { getApiUrl } from '../config/api.js'

const handleRegister = async () => {
  loading.value = true
  try {// 添加调试日志
    const apiUrl = getApiUrl('/auth/register')
    console.log('注册API URL:', apiUrl)
    console.log('当前页面协议:', window.location.protocol)
    console.log('当前页面URL:', window.location.href)
    
    // 注意：请根据实际后端接口地址修改
    await axios.post(apiUrl, { ...registerForm }, {
      headers: { 'Content-Type': 'application/json' }
    })
    loading.value = false
    ElMessage.success('注册成功！即将跳转到登录页面')
    setTimeout(() => {
      router.push('/')
    }, 1500)
  } catch (e) {
    loading.value = false
    console.error('注册失败:', e)
    ElMessage.error('注册失败: ' + (e.response?.data || e.message))

  }
}

// 跳转到登录页
const goToLogin = () => {
  router.push('/')
}
</script>

<style scoped>
.register-container {
  width: 100%;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
}

.register-box {
  width: 100%;
  max-width: 500px;
  padding: 40px 30px;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  margin: auto;
}

.register-title {
  text-align: center;
  margin-bottom: 30px;
  font-size: 24px;
  font-weight: 600;
  color: #303133;

}

.register-button {
  width: 100%;
  font-size: 16px;
  height: 48px;
  margin-top: 10px;
}

.login-link {
  text-align: center;
  margin-top: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    padding: 10px;
    min-height: calc(100vh - 20px);
  }
  
  .register-box {
    padding: 30px 20px;
    max-width: 100%;
  }
  
  .register-title {
    font-size: 20px;
    margin-bottom: 25px;
  }
}

@media (max-width: 480px) {
  .register-container {
    padding: 5px;
    min-height: calc(100vh - 10px);
  }
  
  .register-box {
    padding: 25px 15px;
  }
  
  .register-title {
    font-size: 18px;
    margin-bottom: 20px;
  }
}

/* 当F12开发者工具打开时的特殊处理 */
@media (max-height: 600px) {
  .register-container {
    min-height: auto;
    height: auto;
    padding: 10px;
  }
  
  .register-box {
    padding: 20px 15px;
  }
}

/* 确保在小屏幕上表单元素也能正常显示 */
@media (max-width: 400px) {
  .register-box {
    padding: 20px 10px;
  }
  
  .register-title {
    font-size: 16px;
    margin-bottom: 15px;
  }
}
</style>